package com.comcast.eep.bridge.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.comcast.eep.bridge.model.Consumer;

public class ConsumerDAOImpl extends HibernateDaoSupport implements ConsumerDAO  {

	private static final Log logger = LogFactory.getLog(ConsumerDAOImpl.class);
	
	public Map<String, Consumer> getConsumer(List<String> mcons)  {
		Map<String, Consumer> consMap = new HashMap<String, Consumer>();
		DetachedCriteria criteria = null;
		
		criteria = DetachedCriteria.forClass(Consumer.class).add(Restrictions.in("consumer", mcons));
		getHibernateTemplate().setCacheQueries(true);
		List<Consumer> consumers = getHibernateTemplate().findByCriteria(criteria);
		
		if(logger.isDebugEnabled()) {
			logger.debug("Rows:" + consumers.size());
		}
		
		for(String mcon : mcons) {
			
			for(Consumer consumer : consumers){
				if(! consMap.containsKey(mcon) && mcon.equalsIgnoreCase(consumer.getConsumer())) {
					consMap.put(mcon, consumer);
					break;
				}
			}
		}
		
		return  consMap; 
	}
	
	
}
